<?php

$root = dirname(__FILE__);
define('BASEPATH', $root.'/');
require_once $root.'/function.php';
require_once $root.'/config.php';
require_once $root.'/model.php';


$model = new Model();

//獲取中轉站的server信息
$baseServer = $model->getBaseServer();
initBaseServerInfo($baseServer);

$slist = $model->getStorageServerList(1);

foreach($slist as $storageServer){
  $return = getDiskUseSize();
  if( !$return){
     continue;
  }
  $return['id'] = $storageServer['id'];
//var_dump($return);exit;
  $model->setStorageServerUse($return);
  usleep(3);
}


//var_dump($slist);exit;
foreach($slist as $storageServer){
//var_dump($storageServer);exit;
  if( !isset($storageServer['serverid'])){
    die("\n=====The storageServer serverid is Empty!====\n");
  }
  $flist = scanRemoteDir($storageServer);
//var_dump($flist);exit;
  foreach($flist as $avkey){
    $avinfo = $model->getAvByid($avkey, 0, 0, 1);
// allowstatus 1, 44, 66
    if(isset($avinfo['avkey']) && in_array($avinfo['status'], $allowAvStatus)){
       continue;
    }
//刪除有文件 數據庫沒記錄的情況
    if( !isset($avinfo['avkey'])){
       $avinfo['avkey'] = $avkey;
    }
    $status = deleteAvFilesByAvkey($avinfo);
    usleep(300000);
    echo "\n===== 已經執行 rm $avkey 命令! ===========\n";
    if($status){
      //刪除成功 
      $model->setAvStatusByAvkey($avkey, 8);
    }else{
      $model->setAvStatusByAvkey($avkey, 9);
    }
  } // end /hdd1/ file list
}  //end serverlist
echo "\n===== 任務執行完畢暫停.歇一會繼續! =======\n";
sleep(60);
